import { _decorator, Button, Component, find, instantiate, Label, Node, Prefab } from 'cc';
import { UIBase } from './UIBase';
import { UIRoot } from './UIRoot';
import { gameConfig, scoreData } from './Configs';
import { resMgr } from './Res/ResMgr';
import { PrefabMgr } from './Res/SpecialResMgr';
import { ModuleMgr } from './ModuleMgr';
const { ccclass, property } = _decorator;

@ccclass('GameOver')
export class GameOver extends UIBase {
    private _btnMenu: Node = null;
    private _btnReStart: Node = null;
    private _bg: Node = null;
    private _result: Node = null;
    private _btnClose: Node = null;
    private _bestScore: Label = null;
    private _curScore: Label = null;
    protected onLoad(): void {
        this._bg = this.node.getChildByName("Background");
        this._result = this.node.getChildByName("Result");
        this._btnMenu = this._bg.getChildByName("BtnMenu");
        this._btnReStart = this._bg.getChildByName("BtnReStart");
        this._btnClose = this._result.getChildByName("BtnClose");
        this._bestScore = this._result.getChildByName("BestScore").getComponent(Label);
        this._curScore = this._result.getChildByName("CurScore").getComponent(Label);
        this._btnMenu.on("click", this.onBtnMenu, this);
        this._btnReStart.on("click", this.onReStart, this);
        this._btnClose.on("click", this.onCloseBtn, this);
        this._bestScore.string = `${scoreData.bestScore}`;
        this._curScore.string = `${scoreData.curScore}`;
    }
    start() {

    }

    update(deltaTime: number) {

    }

    onBtnMenu() {
        let gameNode = find("Canvas/Game");
        gameNode.destroy();
        UIRoot.instance.closeUI("MainUI");
        UIRoot.instance.closeUI("GameOver");
        gameConfig.isScroll = false;
        gameConfig.roleIndex = 0;
        gameConfig.fireSpeed = 700;
        gameConfig.horAcc = 0;
        gameConfig.verAcc = -500;
        scoreData.curScore = 0;
        //清空模块
        ModuleMgr.instance.clearModule();
        UIRoot.instance.openUI("Menu");

    }


    onReStart() {
        //移除Game节点
        find("Canvas/Game").destroy();
        UIRoot.instance.closeUI("MainUI");
        UIRoot.instance.closeUI("GameOver");
        gameConfig.isScroll = false;
        gameConfig.fireSpeed = 700;
        gameConfig.horAcc = 0;
        gameConfig.verAcc = -500;
        scoreData.curScore = 0;
        //清空模块
        ModuleMgr.instance.clearModule();
        UIRoot.instance.openUI("MainUI");
        //创建Game界面
        let gamePre = resMgr.getMgr<PrefabMgr>("PrefabMgr").getRes<Prefab>("Game");
        let gameNode = instantiate(gamePre);
        gameNode.parent = find("Canvas");
        //设置Game页面的层级，应该显示在MainUI的下面，MainUI应该一直显示在最上面
        gameNode.setSiblingIndex(-100);
    }

    onCloseBtn() {
        this._result.active = false;
        this._bg.active = true;
    }
}


